package org.gnyxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.gnyxy.constant.DeleteConstant;
import org.gnyxy.constant.MessageConstant;
import org.gnyxy.constant.StatusConstant;
import org.gnyxy.dto.CarouselPageQueryDTO;
import org.gnyxy.entity.Carousel;
import org.gnyxy.exception.BaseException;
import org.gnyxy.mapper.CarouselMapper;
import org.gnyxy.result.PageResult;
import org.gnyxy.service.CarouselService;
import org.gnyxy.vo.CarouselVO;
import org.gnyxy.vo.TypeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CarouselServiceImpl implements CarouselService {
    @Autowired
    private CarouselMapper carouselMapper;

    /**
     * 添加轮播图
     * @param carousel
     */
    @Override
    public void add(Carousel carousel) {
        // 校验是否为空
        if (carousel == null){
            throw new BaseException(MessageConstant.OBJECT_IS_NULL);
        }
        // 设置状态和逻辑删除
//        // 如果不传入status则默认启用
        if (carousel.getStatus() == null){
            carousel.setStatus(StatusConstant.ENABLE);
        }
        carousel.setDeleted(DeleteConstant.IS_NOT_DELETED);
        // mapper执行SQL
        carouselMapper.insert(carousel);
    }

    /**
     * 分页
     * @param carouselPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(CarouselPageQueryDTO carouselPageQueryDTO) {
        PageHelper.startPage(carouselPageQueryDTO.getPage(),carouselPageQueryDTO.getPageSize());
        Page<TypeVO> typeVOS = carouselMapper.pageQuery(carouselPageQueryDTO);
        return new PageResult(typeVOS.getTotal(), typeVOS.getResult());
    }

    /**
     * 更改轮播图信息
     * @param carousel
     * @return
     */
    @Override
    public void update(Carousel carousel) {
        // 校验校验是否为空
        if (carousel == null){
            throw new BaseException(MessageConstant.OBJECT_IS_NULL);
        }
        carouselMapper.update(carousel);
    }

    /**
     * 根据id查询轮播图信息
     * @param id
     * @return
     */
    @Override
    public CarouselVO getById(Long id) {
        // 校验校验是否为空
        if (id == null){
            throw new BaseException(MessageConstant.ID_IS_NULL);
        }
        // 根据id查询
        Carousel carousel = carouselMapper.getById(id);
        // 创建对象并拷贝
        CarouselVO carouselVO = new CarouselVO();
        BeanUtils.copyProperties(carousel,carouselVO);
        return carouselVO;
    }
    /**
     * 删除轮播图
     * @return
     */
    @Override
    public void deleteById(Long id) {
        // 校验校验是否为空
        if (id == null){
            throw new BaseException(MessageConstant.ID_IS_NULL);
        }
        // 查询
        Carousel carousel = carouselMapper.getById(id);
        // 设置删除
        carousel.setDeleted(DeleteConstant.IS_DELETED);
        // 提交
        carouselMapper.update(carousel);
    }
    /**
     * 设置状态
     * @param status
     * @param id
     * @return
     */
    @Override
    public void setStatus(Integer status, Long id) {
        // 校验校验是否为空
        if (id == null || status == null){
            throw new BaseException(MessageConstant.OBJECT_IS_NULL);
        }
        // 查询
        Carousel carousel = carouselMapper.getById(id);
        // 设置状态
        carousel.setStatus(status);
        // 提交更新
        carouselMapper.update(carousel);
    }

    /**
     * 轮播图展示
     * @return
     */
    @Override
    public List<CarouselVO> getAll() {
        List<Carousel> carousels = carouselMapper.getAll();
        return carousels.stream().map(carousel -> {
            CarouselVO carouselVO = new CarouselVO();
            BeanUtils.copyProperties(carousel,carouselVO);
            return carouselVO;
        }).collect(Collectors.toList());
    }
}
